package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.mapper.StudentMapper;
import com.example.pojo.Student;
import com.example.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.pojo.Student;

import java.util.ArrayList;
import java.util.List;

/**
 * @ author:lin tingfeng
 * @ data:2021/9/7-20:10
 */
@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public List<Student> selectStudentByAll() {
        List<Student> students = studentMapper.selectList(null);
        return students;
    }

    @Override
    public List<Student> selectStudentByType(String type, String content) {
        //条件封装
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        if (type.equals("stuid")) {
            queryWrapper.like(StringUtils.isNotBlank(content), "stuid", content);
            return studentMapper.selectList(queryWrapper);
        } else {
            queryWrapper.like(StringUtils.isNotBlank(content), "name", content);
            return studentMapper.selectList(queryWrapper);
        }
    }

    @Override
    public Student selectStudentById(String stuid) {
        Student student = studentMapper.selectById(stuid);
        return student;
    }

    @Override
    public Boolean validateLogin(String stuid, String password) {
        Student student = selectStudentById(stuid);
        if (student != null && student.getPassword().equals(password)) {
            return true;
        }
        return false;
    }

    @Override
    public Boolean insertStudent(Student student) {
        Student stu = studentMapper.selectById(student.getStuid());
        if (stu == null) {
            if (studentMapper.insert(student) != 0)
                return true;
        }
        return false;
    }

    @Override
    public Boolean deleteStudentById(String stuid) {
        Student student = studentMapper.selectById(stuid);
        if (student != null) {
            if (studentMapper.deleteById(stuid) != 0)
                return true;
        }
        return false;
    }


    public Boolean updateStudent(Student updateStu) {
        Student student = studentMapper.selectById(updateStu.getStuid());
        if (student != null) {
            student.setStuid(updateStu.getStuid());
            student.setName(updateStu.getName());
            student.setPassword(updateStu.getPassword());
            student.setSex(updateStu.getSex());
            student.setPower(updateStu.getPower());
            if (studentMapper.updateById(student) != 0)
                return true;
        }
        return false;
    }
}
